package com.nd.order_sys_yhx.controller;


import com.nd.order_sys_yhx.common.result.Result;
import com.nd.order_sys_yhx.common.result.ResultUtils;
import com.nd.order_sys_yhx.entity.Users;
import com.nd.order_sys_yhx.service.IUsersService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yhx
 * @since 2024-12-25
 */
@RestController
@RequestMapping("/users")
public class UsersController {
//    @RequestMapping("/sayHi")
//    public Object sayHi(){
//        return "hello y!";
//    }

    @Autowired//自动导入
    IUsersService service;

    @GetMapping("/")//   "/"为查询all
    public Object getList(){
        return ResultUtils.returnSuccessLayui(service.list(),service.list().size());
    }

    @GetMapping("/{id}")//根据id查询单个
    public Object getById(@PathVariable("id")Integer id){
        return service.getById(id);
    }

    @DeleteMapping("/{id}")
    public Object del(@PathVariable("id")Integer id){
        if(service.removeById(id)){
            return ResultUtils.returnSuccess();
        }
        else return ResultUtils.returnFail("删除失败");
    }

    @PostMapping("/")
    public Object add(@RequestBody  Users users){
        if (service.save(users)){
            return ResultUtils.returnSuccess();
        }
        else return ResultUtils.returnFail("添加失败") ;
    }

    @PutMapping("/")
    public Object update(@RequestBody Users users){
        if(service.updateById(users)){
            return ResultUtils.returnSuccess();
        }
        else return  ResultUtils.returnFail("修改失败");

    }

}
